package sk.velkavrana.app.ui;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import sk.velkavrana.app.context.Context;
import sk.velkavrana.app.ui.components.JConnectionPanel;
import sk.velkavrana.app.util.UIUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author mtymes
 * @since May 25, 2009 12:47:36 AM
 */
public class JMainAppPanel extends JPanel {

    private JConnectionPanel connectionPanel;


    private JButton initImportButton() {
        return new JButton(new AbstractAction("Import") {
            public void actionPerformed(ActionEvent e) {
                // todo: first check if connection is ok

                Context.getInstance().setConnectionInfoEx(connectionPanel.getConnectionInfoEx());

                JDialog dialog = new JDialog((Frame) null, "Import files", true);
                dialog.setSize(400, 450);
                dialog.getContentPane().add(new JImportPanel());
                UIUtil.center(dialog);
                UIUtil.show(dialog);
            }
        });
    }


    private JButton initExportButton() {
        return new JButton(new AbstractAction("Export") {
            public void actionPerformed(ActionEvent e) {
                // todo: first check if connection is ok

                Context.getInstance().setConnectionInfoEx(connectionPanel.getConnectionInfoEx());

                JDialog dialog = new JDialog((Frame) null, "Export tables", true);
                dialog.setSize(400, 300);
                dialog.getContentPane().add(new JExportPanel());
                UIUtil.center(dialog);
                UIUtil.show(dialog);
            }
        });
    }


    private JButton initCloseButton() {
        return new JButton(new AbstractAction("Close") {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }


    private JPanel initButtonPanel() {
        FormLayout buttonLayout = new FormLayout(
                "pref:grow, pref, 4dlu, pref,4dlu, pref, 2dlu", // columns
                "4dlu, pref, 2dlu" // rows
        );

        JPanel buttonPanel = new JPanel(buttonLayout);

        CellConstraints cc = new CellConstraints();

        buttonPanel.add(initImportButton(), cc.xy(2, 2));
        buttonPanel.add(initExportButton(), cc.xy(4, 2));
        buttonPanel.add(initCloseButton(), cc.xy(6, 2));

        return buttonPanel;
    }


    private void init() {
        FormLayout mainAppLayout = new FormLayout(
                "2dlu, pref:grow, 2dlu", // columns
                "2dlu, pref, pref, 4dlu" // rows
        );

        setLayout(mainAppLayout);

        CellConstraints cc = new CellConstraints();

        connectionPanel = new JConnectionPanel();
        connectionPanel.setConnectionInfoEx(Context.getInstance().getConnectionInfoEx());

        add(connectionPanel, cc.xy(2, 2));
        add(initButtonPanel(), cc.xy(2, 3));
    }


    public JMainAppPanel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);

        init();
    }


    public JMainAppPanel(LayoutManager layout) {
        super(layout);

        init();
    }


    public JMainAppPanel(boolean isDoubleBuffered) {
        super(isDoubleBuffered);

        init();
    }


    public JMainAppPanel() {
        init();
    }
}
